﻿using FictionsDownload.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FictionsDownload.Helper
{
    public static class QidianHelper
    {
        /// <summary>
        /// 读取书架的id和书架名
        /// </summary>
        /// <param name="html">书架的Html</param>
        /// <returns>书架的id和书架名</returns>
        public static Dictionary<string, string> GetBookcase(string html)
        {
            string bodyHtml = HtmlHelper.HtmlTrim(html, "<ul class=\"sideBar-group\" id=\"tabView\">", "<a href=\"/bookcase/auto\" class=\"sideBar-list-a\" title=\"自动订阅\" data-eid=\"qd_M188\">");
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            //提取下载链接
            Regex reg = new Regex("<a href=\"/bookcase/(.*?)\" class=\"sideBar-list-a\" title=\"(.*?)\"");
            Match match;
            while (true)
            {
                match = reg.Match(bodyHtml);
                if (match.Success)
                {
                    if (!keyValuePairs.ContainsKey(match.Groups[1].Value))
                    {
                        keyValuePairs.Add(match.Groups[1].Value, match.Groups[2].Value);
                    }
                }
                else
                {
                    break;
                }
                bodyHtml = bodyHtml.Substring(match.Index + match.Length);
            }
            return keyValuePairs;
        }
    }
}
